package org.zl.mybatis.use.meiju;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

/**
 * AppUserStatus：自定义类型处理器
 * @author ben
 * @date 2021-12-16 20:42:10 CST
 */
// 数据库中的数据类型
@MappedJdbcTypes(value = JdbcType.INTEGER)
// 转换后的数据类型
@MappedTypes(value = AppUserStatus.class)
public class AppUserStatusTypeHandler implements TypeHandler<AppUserStatus> {

	private final Map<Integer, AppUserStatus> appUserStatusMap = new HashMap<>(4);
	
	public AppUserStatusTypeHandler() {
		for (AppUserStatus stat : AppUserStatus.values()) {
			appUserStatusMap.put(stat.ordinal(), stat);
		}
	}
	
	@Override
	public void setParameter(PreparedStatement ps, int i, AppUserStatus parameter, JdbcType jdbcType)
			throws SQLException {
		ps.setInt(i, parameter.ordinal());
	}

	@Override
	public AppUserStatus getResult(ResultSet rs, String columnName) throws SQLException {
		Integer value = rs.getInt(columnName);
		return appUserStatusMap.get(value);
	}

	@Override
	public AppUserStatus getResult(ResultSet rs, int columnIndex) throws SQLException {
		Integer value = rs.getInt(columnIndex);
		return appUserStatusMap.get(value);
	}

	@Override
	public AppUserStatus getResult(CallableStatement cs, int columnIndex) throws SQLException {
		Integer value = cs.getInt(columnIndex);
		return appUserStatusMap.get(value);
	}

}
